//
//  HomeDetailPictureCell.m
//  share_business
//
//  Created by 吴于凌 on 2017/9/10.
//  Copyright © 2017年 wyl. All rights reserved.
//

#import "HomeDetailPictureCell.h"

@interface HomeDetailPictureCell (){
    float totalHeight ;
    
}
@property(nonatomic,strong) NSMutableDictionary * picHeightDictionary  ;

@end

@implementation HomeDetailPictureCell


-(void)setUpView {
    _picHeightDictionary = [NSMutableDictionary dictionary];
}
-(void)setModel:(NSArray*)pic{
    totalHeight = 0 ;
    for (int i= 0; i<pic.count; i++) {
        [_picHeightDictionary setObject:[NSNumber numberWithInteger:0] forKey:[NSNumber numberWithInteger:100+i]];
    }
    UIImageView *imageTemp = nil;
    for (int i = 0 ; i< pic.count; i++) {
        UIImageView *imageView = [BSWidget commonImageViewWithFrame:CGRectMake(10,imageTemp?imageTemp.maxY:imageTemp.maxY   , APPWIDTH-20, 0) image:nil];
        imageView.userInteractionEnabled = YES ;
        
        imageView.tag = 100 + i ;
        NSString *urlStr = @"";
        urlStr = [BSCommon getTestImageUrl:pic[i]];
        @weakify(self);
        [BSCommon setImageWithUrlWithBlock:imageView url:urlStr block:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            @strongify(self);
            
            if (image) {
                imageView.height = (APPWIDTH-20) * (image.size.height /image.size.width);
                totalHeight = (APPWIDTH-20) * (image.size.height /image.size.width) ;
                [_picHeightDictionary setObject:[NSNumber numberWithInteger: imageView.height] forKey:[NSNumber numberWithInteger:imageView.tag]];
                [self updateImgPosition];
                if (self.delegate) {
                    [self.delegate updatePictureHeight:totalHeight];
                }
            }
        }];
        imageTemp = imageView ;
        [self.contentView addSubview:imageView];
    }
    
}

-(void)updateImgPosition{
    UIImageView *imageTemp = nil ;
    for (int i = 0 ; i< _picHeightDictionary.count; i++) {
        UIImageView *imageview = [self viewWithTag:100+i];
        imageview.height =  [[_picHeightDictionary objectForKey:[NSNumber numberWithInteger:100+i]]integerValue];
        imageview.y = imageTemp?imageTemp.maxY:0 + imageTemp.maxY ;
        imageTemp = imageview ;
    }
}
@end
